package com.cloudcc.mobile.im.parse;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.mobile.dao.impl.ContactEngineImpl;
import com.cloudcc.mobile.entity.CoworkerEntity;
import com.cloudcc.mobile.im.IMHelper.DataSyncListener;
import com.cloudcc.mobile.im.utils.PreferenceManager;
import com.cloudcc.mobile.manager.ContactsManager;
import com.easemob.EMValueCallBack;
import com.easemob.chat.EMChat;
import com.easemob.easeui.domain.EaseUser;

public class UserProfileManager {

	/**
	 * application context
	 */
	protected Context appContext = null;

	/**
	 * init flag: test if the sdk has been inited before, we don't need to init
	 * again
	 */
	private boolean sdkInited = false;

	/**
	 * HuanXin sync contact nick and avatar listener
	 */
	private List<DataSyncListener> syncContactInfosListeners;

	private boolean isSyncingContactInfosWithServer = false;

	private EaseUser currentUser;

	public UserProfileManager() {
	}

	public synchronized boolean init(Context context) {
		if (sdkInited) {
			return true;
		}
		syncContactInfosListeners = new ArrayList<DataSyncListener>();
		sdkInited = true;
		return true;
	}

	public void addSyncContactInfoListener(DataSyncListener listener) {
		if (listener == null) {
			return;
		}
		if (!syncContactInfosListeners.contains(listener)) {
			syncContactInfosListeners.add(listener);
		}
	}

	public void removeSyncContactInfoListener(DataSyncListener listener) {
		if (listener == null) {
			return;
		}
		if (syncContactInfosListeners.contains(listener)) {
			syncContactInfosListeners.remove(listener);
		}
	}

	public void asyncFetchContactInfosFromServer(List<String> usernames,
			final EMValueCallBack<List<CoworkerEntity>> callback) {
		if (isSyncingContactInfosWithServer) {
			return;
		}
		isSyncingContactInfosWithServer = true;
		new ContactEngineImpl()
				.getContactsSync(new ResultCallBack<List<CoworkerEntity>>() {
					@Override
					public void onSuccess(List<CoworkerEntity> users) {
						isSyncingContactInfosWithServer = false;
						if (!EMChat.getInstance().isLoggedIn()) {
							return;
						}
						ContactsManager.getInstance().saveContactesToDB(users);
						if (callback != null) {
							callback.onSuccess(users);
						}

					}

					@Override
					public void onFailure(ErrorInfo error) {
						isSyncingContactInfosWithServer = false;
						if (callback != null) {
							callback.onError(error.getErrorCode(),
									error.getErrorMessage());
						}
					}
				});
	}

	public void notifyContactInfosSyncListener(boolean success) {
		for (DataSyncListener listener : syncContactInfosListeners) {
			listener.onSyncComplete(success);
		}
	}

	public boolean isSyncingContactInfoWithServer() {
		return isSyncingContactInfosWithServer;
	}

	public synchronized void reset() {
		isSyncingContactInfosWithServer = false;
		currentUser = null;
		PreferenceManager.getInstance().removeCurrentUserInfo();
	}

	public void asyncGetUserInfo(final String username,
			final EMValueCallBack<EaseUser> callback) {
		// ParseManager.getInstance().asyncGetUserInfo(username, callback);
	}

}
